goog.provide('cz.mzk.kramerius.audio.soundmanager2.Playlist');

cz.mzk.kramerius.audio.soundmanager2.Playlist = function(tracks){
    this.tracks = tracks;
    this.nowPlaying = undefined;
}

cz.mzk.kramerius.audio.soundmanager2.Playlist.prototype.getTrack_ = function(position){
    if (position >= this.tracks.length){
        console.log("position (" + position + ") >= " + " playlist length (" + this.tracks.length + ")");
        return 0;
    }else{
        return this.tracks[position];
    }
}

cz.mzk.kramerius.audio.soundmanager2.Playlist.prototype.play = function(playlistId){
    //If SoundManager object exists, get rid of it...
    if (this.nowPlaying){
        this.nowPlaying.destruct();
        // ...and reset array key if end reached
        if(playlistId == this.tracks.length){
            playlistId = 0;
        }
    }
    
    var track = this.getTrack_(playlistId);
    var playlist = this;
    console.log("track : " + track.title + ", " + track.trackArray());
    soundManager.onready(function() {
        //  this.nowPlaying = soundManager.createSound({
        playlist.nowPlaying = soundManager.createSound({
            id: track.title,
            url: track.trackArray(),
            autoLoad: true,
            autoPlay: true,
            volume: 50,
            multiShotEvents: true,
            // ...with a recursive callback when play completes
            onfinish: function(){
                playlistId ++;
                console.log('moving to next track: ' + playlistId);
                //    tracks.play(playlistId);
                playlist.play(playlistId);
            }
        });
        playlist.playing = true;
    });
} 

cz.mzk.kramerius.audio.soundmanager2.Playlist.prototype.pause = function(){
    this.nowPlaying.pause();
}

cz.mzk.kramerius.audio.soundmanager2.Playlist.prototype.resume = function(){
    this.nowPlaying.resume();
}






